﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using Resto.CostControl.Core;
using Resto.CostControl.Core.DWZAdapter;
using EfSearchModel.Model;
using System.Data.Entity.Validation;

namespace Resto.CostControl.Web.Controllers
{
    public class TempDishOrderController : Controller
    {
        private readonly IRepository<TempDishOrder> _Repo;
        private readonly IRepository<DishOrderItem> _RepoItem;
        private readonly IUnitOfWork _UnitOfWork;

        public TempDishOrderController(IRepository<TempDishOrder> repo, IRepository<DishOrderItem> repoItem, UnitOfWork unitOfWork)
        {
            _Repo = repo;
            _RepoItem = repoItem;
            _UnitOfWork = unitOfWork;
        }

        //
        // GET: /Material/

        public ActionResult Index(DWZPageInfo pageInfo, QueryModel where)
        {
            var list = _Repo.GetPageable(pageInfo, where);

            ViewBag.Total = list.Total;
            ViewBag.PageNum = pageInfo.PageIndex + 1;
            ViewBag.NumPerPage = pageInfo.PageSize;
            ViewBag.OrderField = pageInfo.SortName;
            ViewBag.OrderDirection = pageInfo.SortOrder;
            ViewBag.Keywords = pageInfo.Keywords;
            ViewBag.SearchData = pageInfo.SearchData;

            return View(list.Page);
        }

        public ActionResult Edit(string Id)
        {
            TempDishOrder data = null;
            if (!string.IsNullOrWhiteSpace(Id))
            {
                data = _Repo.Find(Id);
                data.Details = _RepoItem.FindAll(m => m.OrderId == Id).ToList();
            }
            else
            {
                data = new TempDishOrder();
                data.Details = new List<DishOrderItem>();
            }
            return View(data);
        }

        [HttpPost]
        public JsonResult Save(TempDishOrder data, IList<DishOrderItem> details)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(data.Id))
                    {
                        data.Id = Guid.NewGuid().ToString();
                        _Repo.Insert(data);
                    }
                    else
                    {
                        data = _Repo.Find(data.Id);
                        TryUpdateModel(data);

                        _RepoItem.DeleteAll(m => m.OrderId == data.Id);
                    }

                    foreach (var m in details)
                    {
                        var item = new DishOrderItem
                        {
                            Id = Guid.NewGuid().ToString(),
                            OrderId = data.Id,
                            Type = m.Type,
                            Code = m.Code,
                            Name = m.Name,
                            CostPrice = m.CostPrice,
                            GuestCount = m.GuestCount,
                        };

                        _RepoItem.Insert(item);
                    }

                    _UnitOfWork.Commit();
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "TempDishOrder",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }

        [HttpPost]
        public JsonResult DeleteItem(string Id)
        {
            var result = new FormReturnJson
            {
                statusCode = "200",
                message = "操作成功",
                navTabId = "TempDishOrder",
                callbackType = "closeCurrent",
                forwardUrl = "",
            };
            return Json(result);
        }
    }
}